package com.hs.telepass.utils.security;

import android.content.Context;
import android.util.Base64;

import javax.crypto.Cipher;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;

public class Cert {
    private PublicKey pubKey;
    private Certificate cert;
    public Cert(Context c, String file) {
        try {
            InputStream inputStream = c.getAssets().open(file);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            // Use BufferedInputStream (which supports mark and reset) so that each
            // generateCertificate call consumes one certificate.
            //BufferedInputStream in = new BufferedInputStream(inputStream);
			/*
			while (in.available() > 0) {
			    Certificate cert = cf.generateCertificate(in);
			    Log.e("Cert: ", cert.toString());
			}
			*/
            cert = cf.generateCertificate(inputStream);
            //in.close();
            inputStream.close();
			/*
			Log.e("Cert: ", "Certificate Object Info: ");
		    Log.e("Cert: ", "Type = "+cert.getType());
		    Log.e("Cert: ", "toString = "+cert.toString());
		    Log.e("Cert: ", cert.toString());
		    */
            pubKey= cert.getPublicKey();
			/*
		    Log.e("Cert: ","PublicKey Object Info: ");
		    Log.e("Cert: ","Algorithm = "+pubKey.getAlgorithm());
		    Log.e("Cert: ","Format = "+pubKey.getFormat());
		    Log.e("Cert: ","toString = "+pubKey.toString());
		    */

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Cert(String s) {
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X509");
            InputStream in = new ByteArrayInputStream(Base64.decode(s, Base64.DEFAULT));
            cert = cf.generateCertificate(in);
            in.close();
            pubKey= cert.getPublicKey();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public Cert(InputStream in) {
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X509");
            cert = cf.generateCertificate(in);
            in.close();
            pubKey= cert.getPublicKey();
        } catch (Exception e) {
        }
    }
    public Certificate getCert() {
        return this.cert;
    }
    public String pubKeyEncrypt(String text) {
        String output = null;
        //pubKeyByte
        try{
//            Cipher cipher = Cipher.getInstance("RSA");
//            Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
//            Cipher cipher = Cipher.getInstance("RSA/None/NoPadding");
            Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding");
            cipher.init( Cipher.ENCRYPT_MODE , pubKey );

            //getPublicKey(new File("/data/data/" + Const.PACKAGE_NAME + "/databases/pub.der"));
            //cipher.init( Cipher.ENCRYPT_MODE , RpubKey );

            output = Base64Utils.encode(cipher.doFinal(text.getBytes()));
        }catch(Exception e){
            //Core.debug("pubKeyEncrypt: "+e);
            e.printStackTrace();
        }
        return output;
        //return encrypt(Base64.decode(text, Base64.DEFAULT));
    }

}